﻿using System;
using System.Net;
using System.Windows;
using System.Windows.Controls;
using System.Windows.Documents;
using System.Windows.Ink;
using System.Windows.Input;
using System.Windows.Media;
using System.Windows.Media.Animation;
using System.Windows.Shapes;

namespace TimeZoneTest.ViewModel.Base
{
  

   /// <summary>
   /// Delegate command - does it all
   /// </summary>
   public class DelegateCommand<T> : BaseDelegateCommand
   {
      private readonly Action<T> _execute = obj => { };
      private readonly Func<T, bool> _canExecute = obj => true;

      public DelegateCommand()
      {

      }

      /// <summary>
      ///     Constructor with action to perform
      /// </summary>
      /// <param name="execute">The action to execute</param>
      public DelegateCommand(Action<T> execute)
      {
         _execute = execute;
      }

      /// <summary>
      ///     Constructor with action and condition
      /// </summary>
      /// <param name="execute"></param>
      /// <param name="canExecute"></param>
      public DelegateCommand(Action<T> execute, Func<T, bool> canExecute)
      {
         _execute = execute;
         _canExecute = canExecute;
        
      }

      /// <summary>
      /// Defines the method that determines whether the command can execute in its current state.
      /// </summary>
      /// <returns>
      /// true if this command can be executed; otherwise, false.
      /// </returns>
      /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null. </param>
      public override bool CanExecute(object parameter)
      {
         return _canExecute((T)parameter);
      }

      /// <summary>
      /// Defines the method to be called when the command is invoked.
      /// </summary>
      /// <param name="parameter">Data used by the command. If the command does not require data to be passed, this object can be set to null. </param>
      public override void Execute(object parameter)
      {
         if (CanExecute(parameter))
         {
            _execute((T)parameter);
         }
      }

   }
}
